home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 293_01 / cttga.c < prev    next >
C/C++ Source or Header  |  1989-08-23  |  5KB  |  165 lines

  1. /*
  2.  * Converts a 256*256 ct file into a 256*256 TARGA-TIPS B/W file.
  3.  *
  4.  * Usage: cttga infile outfile [-l] [-u] [-h] [-s]
  5.  *        -l    : lower threshold (default =  0)
  6.  *        -u    : upper threshold (default =  255)
  7.  *        -h    : number of header blocks (512 bytes) (default = 1)
  8.  *        -s    : output statistics
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #ifdef MSDOS
  15. typedef int WORD;
  16. #else
  17. typedef unsigned short int WORD;
  18. #endif
  19. #ifndef SEEK_SET
  20. #define SEEK_SET 0
  21. #endif
  22.  
  23. /* allocate all arrays as global */
  24. char  fni[256], fno[256];
  25. WORD   inline[256];
  26. unsigned char outline[256];
  27. char  *usestr = "Usage: cttga infile outfile [-l] [-u] [-h] [-s]";
  28.  
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.   FILE *fpi, *fpo;
  35.   int   lower;
  36.   int   upper;
  37.   int   range;
  38.   WORD  tgaheadi;
  39.   unsigned char tgaheadc;
  40.   int   line, pixel;
  41.   int   statistics;
  42.   int   minidens, maxidens;
  43.   int   minodens, maxodens;
  44.   int   header_blocks;
  45.  
  46.   fni[0] = fno[0] = '\0';
  47.   statistics = 0;
  48.   lower = 0;
  49.   upper = 255;
  50.   header_blocks = 1;
  51.   for (argc; argc > 1; argc--) {
  52.     if (**++argv == '-' || **argv == '/') {
  53.       switch (tolower(*(*argv + 1))) {
  54.         case 'l':
  55.           lower = atoi(*argv+2);
  56.           break;
  57.         case 'u':
  58.           upper = atoi(*argv+2);
  59.           break;
  60.         case 'h':
  61.           header_blocks = atoi(*argv+2);
  62.           break;
  63.         case 's':
  64.           statistics = 1;
  65.           break;
  66.         default:
  67.           printf("%s\n", usestr);
  68.           exit(0);
  69.           break;
  70.       }
  71.     }
  72.     else {
  73.       if (fni[0] == '\0')
  74.         strcpy(fni, *argv);
  75.       else
  76.         strcpy(fno, *argv);
  77.     }
  78.   }
  79.  
  80.   if (fni[0] == '\0' || fno[0] == '\0') {
  81.     printf("%s\n", usestr);
  82.     exit(0);
  83.   }
  84.  
  85.   if ((fpi = fopen(fni, "rb")) == NULL) {
  86.     printf("Error opening %s\n", fni);
  87.     exit(1);
  88.   }
  89.   if ((fpo = fopen(fno, "wb")) == NULL) {
  90.     printf("Error opening %s\n", fno);
  91.     exit(1);
  92.   }
  93.  
  94.   /* skip header in ct file */
  95.   fseek(fpi, (long) (header_blocks*512), SEEK_SET);
  96.  
  97.   range = upper - lower;
  98.   if (range <= 0) {
  99.     printf("lower/upper are out of range (%d)\n", range);
  100.     exit(1);
  101.   }
  102.   if (range < 255)
  103.     printf("lower/upper range gives non optimal contrast (%d)\n", range);
  104.  
  105.   /* write header to tga file */
  106.   tgaheadc = 0;
  107.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  108.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  109.   tgaheadc = 3;
  110.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  111.   tgaheadc = 0;
  112.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  113.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  114.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  115.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  116.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  117.   tgaheadi = 0;
  118.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  119.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  120.   tgaheadi = 256;
  121.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  122.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  123.   tgaheadc = 8;
  124.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  125.   tgaheadc = 0;
  126.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  127.  
  128.   /* read in the file one line at a time and convert the density values */
  129.   minidens = minodens = 16000;
  130.   maxidens = maxodens = -16000;
  131.   for (line = 0; line < 256; line++) {
  132.     fread(inline, sizeof(WORD), 256, fpi);
  133.     for (pixel = 0; pixel < 256; pixel++) {
  134.       minidens = (minidens < inline[pixel]) ? minidens : inline[pixel];
  135.       maxidens = (maxidens > inline[pixel]) ? maxidens : inline[pixel];
  136.       if (inline[pixel] < lower)
  137.         if (inline[pixel] < -16000)
  138.           outline[pixel] = 255;
  139.         else
  140.           outline[pixel] = 0;
  141.       else if (inline[pixel] > upper)
  142.         outline[pixel] = 255;
  143.       else {
  144.         outline[pixel] = 
  145.           (float) (inline[pixel] - lower) / (float) range * 255.;
  146.       }
  147.       minodens = (minodens < (int) outline[pixel]) ? 
  148.                   minodens : (int) outline[pixel];
  149.       maxodens = (maxodens > (int) outline[pixel]) ?
  150.                   maxodens : (int) outline[pixel];
  151.     }
  152.     fwrite(outline, sizeof(unsigned char), 256, fpo);
  153.   }
  154.  
  155.   fclose(fpi);
  156.   fclose(fpo);
  157.  
  158.   if (statistics) {
  159.     printf("input density  - minimum = %5d, maximum = %5d\n",
  160.             minidens, maxidens);
  161.     printf("output density - minimum = %5d, maximum = %5d\n",
  162.             minodens, maxodens);
  163.   } 
  164.  
  165. }